package cn.player.adapter;

import java.util.List;

import net.tsz.afinal.FinalDb;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.player.App;
import cn.player.R;
import cn.player.media.adapter.BaseAdpt;
import cn.player.media.entity.LiveEpg;
import cn.player.media.util.DateUtil;

public class RightEpgInfoAdapter  extends BaseAdpt<LiveEpg.Program> {
	
	private long currentTime;
	private long currentPlayTime;
	private String channelId;
	private App app;
	private FinalDb finalDb;

	public RightEpgInfoAdapter(Context mContext, List<LiveEpg.Program> mData, int layoutResource, String channelId) {
		super(mContext, mData, layoutResource);
		// TODO Auto-generated constructor stub
		app = (App) mContext.getApplicationContext();
		finalDb = app.getFinalDb();
		this.channelId = channelId;
		currentTime = System.currentTimeMillis();
		currentPlayTime = System.currentTimeMillis();
	}
	

	public long getCurrentPlayTime() {
		return currentPlayTime;
	}


	public void setCurrentPlayTime(long currentPlayTime) {
		this.currentPlayTime = currentPlayTime;
	}


	@Override
	protected void addViewId() {
		// TODO Auto-generated method stub
		mViewIds.add(R.id.ll_right_select);
		mViewIds.add(R.id.tv_current_position);
		mViewIds.add(R.id.tv_epg_time);
		mViewIds.add(R.id.tv_epg_title);
		mViewIds.add(R.id.iv_epg_subscribe);
	}

	@Override
	protected void updateItemViews(int position) {
		// TODO Auto-generated method stub
		LinearLayout ll_select = (LinearLayout) viewHolder.getView(R.id.ll_right_select);
		TextView tv_current = (TextView) viewHolder.getView(R.id.tv_current_position);
		TextView tv_epg_time = (TextView)viewHolder.getView(R.id.tv_epg_time);
		TextView tv_epg_title = (TextView)viewHolder.getView(R.id.tv_epg_title);
		ImageView iv_handle = (ImageView)viewHolder.getView(R.id.iv_epg_subscribe);
		tv_epg_time.setText(DateUtil.getTimeString(mData.get(position).getSt(), "HH:mm"));
		tv_epg_title.setText(mData.get(position).getT());
		if(currentTime > mData.get(position).getEt()*1000){
			iv_handle.setBackgroundResource(R.drawable.epg_back_normal);
		}
		
		if(currentTime < mData.get(position).getSt()*1000){
			if(mData.get(position).isSubscribe()){
				iv_handle.setBackgroundResource(R.drawable.epg_subscribe_press);
			}else{
				iv_handle.setBackgroundResource(R.drawable.epg_subscribe_normal);
			}
		}
		
		if(currentPlayTime > mData.get(position).getSt()*1000 &&  currentPlayTime < mData.get(position).getEt()*1000){
			tv_current.setVisibility(View.VISIBLE);
			iv_handle.setBackgroundResource(R.drawable.epg_back_press);
		}else{
			tv_current.setVisibility(View.GONE);
		}
		
		if(currentTime > mData.get(position).getSt()*1000 &&  currentTime < mData.get(position).getEt()*1000){
			iv_handle.setBackgroundResource(R.drawable.epg_living_press);
		}
		
		final int fPosition = position;
		ll_select.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				LiveEpg.Program program = mData.get(fPosition);
				int pId = generatePId(channelId, program.getSt(), program.getT());//用户点击预约，生成pId
			}
		});
		
	}
	
	
	private boolean isProgramSubscribe(long programId){
		//List<Program> programs = app.Db().findAllByWhere(Program.class, null);
		List<LiveEpg.Program> programs = finalDb.findAllByWhere(LiveEpg.Program.class, "program=" + programId);
		if(programs != null && programs.size() > 0){
			return true;
		}
		return false;
		
	}
	
	private int generatePId(String channelId,long startTime,String ptitle){
		int hashCodePId = 0; 
		try {
			hashCodePId =  (channelId + startTime + ptitle).hashCode() ;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hashCodePId;
	}

}
